package com.guagua.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guagua.blog.entity.Article;
import com.guagua.blog.entity.ArticleTag;
import com.guagua.blog.mapper.ArticleMapper;
import com.guagua.blog.mapper.ArticleTagMapper;
import com.guagua.blog.service.ArticleService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@RequiredArgsConstructor
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements ArticleService {
    
    private final ArticleMapper articleMapper;
    private final ArticleTagMapper articleTagMapper;
    
    @Override
    public Article getArticleDetail(Long id) {
        // 增加浏览量
        articleMapper.incrementViewCount(id);
        
        // 查询文章详情
        return articleMapper.selectArticleDetail(id);
    }

    @Override
    public long sumViewCount(Long userId) {
        return lambdaQuery()
            .eq(Article::getAuthorId, userId)
            .select(Article::getViewCount)
            .list()
            .stream()
            .mapToLong(Article::getViewCount)
            .sum();
    }

    @Override
    public List<Article> getHotArticles() {
        return lambdaQuery()
            .orderByDesc(Article::getViewCount)
            .last("LIMIT 10")
            .list();
    }

    @Override
    public IPage<Article> getFavoriteArticles(Long userId, int page, int size) {
        Page<Article> pageParam = new Page<>(page, size);
        return baseMapper.selectFavoriteArticles(pageParam, userId);
    }

    @Override
    public IPage<Article> getReadArticles(Long userId, int page, int size) {
        Page<Article> pageParam = new Page<>(page, size);
        return baseMapper.selectReadArticles(pageParam, userId);
    }

    @Override
    public IPage<Article> getLikedArticles(Long userId, int page, int size) {
        Page<Article> pageParam = new Page<>(page, size);
        return baseMapper.selectLikedArticles(pageParam, userId);
    }

    @Override
    public IPage<Article> getArticlesByTag(Long tagId, int page, int size) {
        Page<Article> pageParam = new Page<>(page, size);
        return baseMapper.selectArticlesByTag(pageParam, tagId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveArticleTags(Long articleId, List<Long> tagIds) {
        // 先删除原有的标签关联
        articleTagMapper.delete(new LambdaQueryWrapper<ArticleTag>()
            .eq(ArticleTag::getArticleId, articleId));
        
        // 保存新的标签关联
        for (Long tagId : tagIds) {
            ArticleTag articleTag = new ArticleTag();
            articleTag.setArticleId(articleId);
            articleTag.setTagId(tagId);
            articleTagMapper.insert(articleTag);
        }
    }

    @Override
    public IPage<Article> getUserArticles(Long userId, int page, int size) {
        Page<Article> pageParam = new Page<>(page, size);
        return baseMapper.selectUserArticles(pageParam, userId);
    }
} 